package c.j.a.c;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import c.j.a.c.g.y;
import c.j.a.d.a.h.f;
import c.j.a.d.b.d.a0;
import c.j.a.d.b.e.i;
import c.j.a.d.b.n.d0;
import com.huawei.agconnect.exception.AGCServerException;
import com.huawei.hms.iap.entity.OrderStatusCode;
import com.huawei.hms.jos.games.ranking.RankingConst;
import com.huawei.hms.support.api.entity.pay.HwPayConstant;
import java.util.Objects;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: BaseDownloadMonitorListener */
public class m implements c.j.a.d.a.h.d {
    public Handler a = new Handler(Looper.getMainLooper());

    /* compiled from: BaseDownloadMonitorListener */
    public class a implements Runnable {
        public final /* synthetic */ c.j.a.d.b.l.a a;
        public final /* synthetic */ c.j.a.a.a.c.d b;

        public a(m mVar, c.j.a.d.b.l.a aVar, c.j.a.a.a.c.d dVar) {
            this.a = aVar;
            this.b = dVar;
        }

        public void run() {
            y.q().a(2, y.a(), this.b, this.a.g("no_enough_space_toast_text", "您的存储空间不足，请清理后再试"), null, 0);
        }
    }

    /* compiled from: AppDownloadLaunchResumeListener */
    public class c {
        public void a(com.ss.android.socialbase.downloader.g.c cVar, boolean z) {
            int p0 = cVar.p0();
            c.j.a.c.g$g.d.c.a.f();
            c.j.a.b.a.c.a b = c.j.a.c.g$g.d.c.a.b(cVar);
            if (b == null) {
                c.j.a.c.q.d.n();
                return;
            }
            if (z) {
                try {
                    b.x = cVar.V();
                } catch (Throwable th) {
                    th.printStackTrace();
                    return;
                }
            } else if (b.x != -1) {
                b.x = -1;
            } else {
                return;
            }
            c.j.a.c.g$g.g.b.a.a(b);
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("download_id", cVar.g0());
            jSONObject.put(RankingConst.RANKING_JGW_NAME, cVar.b);
            jSONObject.put(HwPayConstant.KEY_URL, cVar.d);
            jSONObject.put("download_time", cVar.X);
            jSONObject.put("download_status", p0);
            jSONObject.put("cur_bytes", cVar.I());
            jSONObject.put("total_bytes", cVar.T);
            int i = 1;
            jSONObject.put("only_wifi", cVar.g ? 1 : 0);
            jSONObject.put("chunk_count", cVar.R);
            String str = "launch_resumed";
            if (!z) {
                i = 2;
            }
            jSONObject.put(str, i);
            jSONObject.put("failed_resume_count", cVar.V());
            c.j.a.c.o.b.b.a.q("embeded_ad", "download_uncompleted", jSONObject, b);
        }
    }

    /* compiled from: NewDownloadCompletedEventDispatcher */
    public class d {
        public Context a;

        public d(Context context) {
            this.a = context.getApplicationContext();
        }

        public void a(int i, int i2) {
            int i3 = i2;
            c.j.a.c.o.b bVar = c.j.a.c.o.b.b.a;
            Context context = this.a;
            if (context != null) {
                com.ss.android.socialbase.downloader.g.c k = i.a(context).k(i);
                if (!(k == null || k.o0() == 0)) {
                    c.j.a.b.a.c.a b = c.j.a.c.g$g.d.c.a.b(k);
                    if (b == null) {
                        c.j.a.c.q.d.n();
                    } else if (i3 == 1) {
                        g.k(k, b);
                        if ("application/vnd.android.package-archive".equals(k.u)) {
                            if (c.j.a.c.g.e.c == null) {
                                synchronized (c.j.a.c.g.e.class) {
                                    if (c.j.a.c.g.e.c == null) {
                                        c.j.a.c.g.e.c = new c.j.a.c.g.e();
                                    }
                                }
                            }
                            c.j.a.c.g.e eVar = c.j.a.c.g.e.c;
                            long j = b.a;
                            long j2 = b.b;
                            String str = b.e;
                            String j0 = k.j0();
                            String str2 = b.c;
                            String m0 = k.m0();
                            Objects.requireNonNull(eVar);
                            c.j.a.c.g$g.a aVar = r7;
                            c.j.a.c.g$g.a aVar2 = new c.j.a.c.g$g.a((long) k.g0(), j, j2, str, j0, str2, m0);
                            c.j.a.d.b.l.a d = c.j.a.d.b.l.a.d(k.g0());
                            if (d.b("back_miui_silent_install", 1) == 0 && ((d0.j() || d0.k()) && c.j.a.c.y.a.i.q(y.a(), "com.miui.securitycore", "com.miui.enterprise.service.EntInstallService"))) {
                                if (k.Y().getBoolean("extra_silent_install_succeed", false)) {
                                    Message obtainMessage = eVar.a.obtainMessage(AGCServerException.OK, aVar);
                                    obtainMessage.arg1 = 2;
                                    eVar.a.sendMessageDelayed(obtainMessage, (long) d.b("check_silent_install_interval", OrderStatusCode.ORDER_STATE_CANCEL));
                                } else {
                                    c.j.a.b.a.c.a g = c.j.a.c.g$g.d.c.a.g(aVar.b);
                                    JSONObject jSONObject = new JSONObject();
                                    int i4 = -1;
                                    try {
                                        jSONObject.put("ttdownloader_type", "miui_silent_install");
                                        jSONObject.put("ttdownloader_message", "miui_silent_install_failed: has not started service");
                                        i4 = 5;
                                    } catch (Exception unused) {
                                    }
                                    y.s().a(null, new com.ss.android.socialbase.downloader.e.a(i4, jSONObject.toString()), i4);
                                    bVar.q("embeded_ad", "anti_hijack_result", jSONObject, g);
                                }
                            }
                            Object obj = 1;
                            if (y.t().optInt("is_enable_start_install_again") != 1) {
                                obj = null;
                            }
                            if (obj != null) {
                                long currentTimeMillis = System.currentTimeMillis() - eVar.b;
                                long optLong = y.t().optLong("start_install_interval");
                                if (optLong == 0) {
                                    optLong = 300000;
                                }
                                String str3 = "next_install_min_interval";
                                long optLong2 = y.t().optLong(str3);
                                long j3 = 10000;
                                if (optLong2 == 0) {
                                    optLong2 = 10000;
                                }
                                if (currentTimeMillis < optLong2) {
                                    optLong2 = y.t().optLong(str3);
                                    if (optLong2 != 0) {
                                        j3 = optLong2;
                                    }
                                    j3 -= currentTimeMillis;
                                    optLong += j3;
                                    eVar.b = System.currentTimeMillis() + j3;
                                } else {
                                    eVar.b = System.currentTimeMillis();
                                }
                                c.j.a.c.q.e eVar2 = eVar.a;
                                eVar2.sendMessageDelayed(eVar2.obtainMessage(AGCServerException.OK, aVar), optLong);
                            }
                        }
                    } else if (i3 == 3) {
                        bVar.q("download_notification", "download_notification_install", g.g(new JSONObject(), k), b);
                    } else if (i3 == 5) {
                        bVar.k("download_notification", "download_notification_pause", b);
                    } else if (i3 == 6) {
                        bVar.k("download_notification", "download_notification_continue", b);
                    } else if (i3 == 7) {
                        bVar.k("download_notification", "download_notification_click", b);
                    }
                }
            }
        }

        public void b(com.ss.android.socialbase.downloader.g.c cVar) {
            c.j.a.c.o.b bVar = c.j.a.c.o.b.b.a;
            if (cVar != null) {
                x b = x.b();
                b.a.post(new w(b, cVar));
                int i = 1;
                String str = "";
                if (c.j.a.d.b.l.a.d(cVar.g0()).b("report_download_cancel", 1) == 1) {
                    com.ss.android.socialbase.downloader.e.a aVar = new com.ss.android.socialbase.downloader.e.a(1012, str);
                    c.j.a.b.a.c.a b2 = c.j.a.c.g$g.d.c.a.b(cVar);
                    if (b2 == null) {
                        c.j.a.c.q.d.n();
                    } else if (!b2.J.get()) {
                        JSONObject jSONObject = new JSONObject();
                        try {
                            g.n(jSONObject, cVar);
                            jSONObject.putOpt("fail_status", Integer.valueOf(b2.y));
                            jSONObject.putOpt("fail_msg", b2.z);
                            jSONObject.put("download_failed_times", b2.s);
                            if (cVar.T > 0) {
                                jSONObject.put("download_percent", ((double) cVar.I()) / ((double) cVar.T));
                            }
                            jSONObject.put("download_status", cVar.p0());
                            long currentTimeMillis = System.currentTimeMillis();
                            long j = b2.o;
                            if (j > 0) {
                                jSONObject.put("time_from_start_download", currentTimeMillis - j);
                            }
                            if (b2.e() > 0) {
                                jSONObject.put("time_from_download_resume", currentTimeMillis - b2.e());
                            }
                            jSONObject.put("is_update_download", b2.A ? 1 : 2);
                            jSONObject.put("can_show_notification", c.j.a.d.a.j.e.a() ? 1 : 2);
                            String str2 = "has_send_download_failed_finally";
                            if (!b2.K.get()) {
                                i = 2;
                            }
                            jSONObject.put(str2, i);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        bVar.q(b2.D, "download_cancel", jSONObject, b2);
                    }
                } else {
                    bVar.p(cVar, new com.ss.android.socialbase.downloader.e.a(1012, str));
                }
            }
        }
    }

    /* compiled from: ApkUpdateManager */
    public class b implements a0 {
        public void a(com.ss.android.socialbase.downloader.g.c cVar) throws com.ss.android.socialbase.downloader.e.a {
        }

        public boolean b(com.ss.android.socialbase.downloader.g.c cVar) {
            return cVar != null ? c.h.a.m0.r.a.o(c.j.a.d.b.l.a.d(cVar.g0()), cVar.u) : false;
        }
    }

    /* compiled from: NewDownloadDepend */
    public class e extends c.j.a.d.a.h.b {
        public static String a = "m$e";

        /* compiled from: NewDownloadDepend */
        public class a implements f {
            public c.j.a.a.a.e.c.b a;
            public OnClickListener b;
            public OnClickListener c;
            public OnCancelListener d;
            public final /* synthetic */ Context e;

            /* compiled from: NewDownloadDepend */
            public class a implements c.j.a.a.a.e.c.c {
                public void a(DialogInterface dialogInterface) {
                    OnCancelListener onCancelListener = a.this.d;
                    if (onCancelListener != null && dialogInterface != null) {
                        onCancelListener.onCancel(dialogInterface);
                    }
                }

                public void b(DialogInterface dialogInterface) {
                    OnClickListener onClickListener = a.this.c;
                    if (onClickListener != null) {
                        onClickListener.onClick(dialogInterface, -2);
                    }
                }

                public void c(DialogInterface dialogInterface) {
                    OnClickListener onClickListener = a.this.b;
                    if (onClickListener != null) {
                        onClickListener.onClick(dialogInterface, -1);
                    }
                }
            }

            public a(e eVar, Context context) {
                this.e = context;
                this.a = new c.j.a.a.a.e.c.b(context);
            }

            public f a(int i) {
                this.a.b = this.e.getResources().getString(i);
                return this;
            }

            public f b(int i, OnClickListener onClickListener) {
                this.a.e = this.e.getResources().getString(i);
                this.c = onClickListener;
                return this;
            }

            public f c(int i, OnClickListener onClickListener) {
                this.a.d = this.e.getResources().getString(i);
                this.b = onClickListener;
                return this;
            }

            public f a(String str) {
                this.a.c = str;
                return this;
            }

            public f a(OnCancelListener onCancelListener) {
                this.d = onCancelListener;
                return this;
            }

            public f a(boolean z) {
                this.a.f = z;
                return this;
            }

            public c.j.a.d.a.h.e a() {
                c.j.a.a.a.e.c.b bVar = this.a;
                bVar.h = new a();
                String str = e.a;
                bVar.i = 3;
                return new b(y.q().b(this.a.a()));
            }
        }

        /* compiled from: NewDownloadDepend */
        public static class b implements c.j.a.d.a.h.e {
            public Dialog a;

            public b(Dialog dialog) {
                if (dialog != null) {
                    this.a = dialog;
                    a();
                }
            }

            public void a() {
                Dialog dialog = this.a;
                if (dialog != null) {
                    dialog.show();
                }
            }

            public boolean b() {
                Dialog dialog = this.a;
                return dialog != null ? dialog.isShowing() : false;
            }
        }

        public f a(Context context) {
            return new a(this, context);
        }

        public boolean b() {
            return true;
        }
    }

    public void a(com.ss.android.socialbase.downloader.g.c cVar, com.ss.android.socialbase.downloader.e.a aVar, int i) {
        c.j.a.c.o.b bVar = c.j.a.c.o.b.b.a;
        if (cVar != null) {
            c.j.a.b.a.c.a b = c.j.a.c.g$g.d.c.a.b(cVar);
            if (b != null) {
                if (i == -1) {
                    com.ss.android.socialbase.downloader.e.a aVar2 = null;
                    String str = "";
                    if (aVar != null) {
                        try {
                            if (c.j.a.d.b.n.a.W(aVar)) {
                                bVar.i("download_failed_for_space", b);
                                if (!b.I) {
                                    b.I = true;
                                    bVar.i("download_can_restart", b);
                                    if ((c.j.a.d.b.l.a.d(cVar.g0()).b("exec_clear_space_switch", 0) == 1 ? 1 : null) == null) {
                                        bVar.i("clean_space_switch_closed", b);
                                    } else {
                                        bVar.i("cleanspace_switch_open", b);
                                        c.j.a.c.o.a.a.c(new c.j.a.c.g$i.b(cVar));
                                    }
                                }
                                c.j.a.a.a.c.d a = c.j.a.c.g$g.d.c.a.a(b.a);
                                if (a != null && a.k()) {
                                    c.j.a.d.b.l.a d = c.j.a.d.b.l.a.d(cVar.g0());
                                    if (d.b("show_no_enough_space_toast", 0) == 1) {
                                        this.a.post(new a(this, d, a));
                                    }
                                }
                            }
                            i = y.t().optInt("exception_msg_length", AGCServerException.UNKNOW_EXCEPTION);
                            String message = aVar.getMessage();
                            if (i == 0) {
                                message = str;
                            } else if (!TextUtils.isEmpty(message)) {
                                if (message.length() > i) {
                                    message = message.substring(0, i);
                                }
                            }
                            aVar2 = new com.ss.android.socialbase.downloader.e.a(aVar.a, message);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    bVar.p(cVar, aVar2);
                    x b2 = x.b();
                    b2.a.post(new t(b2, cVar, aVar, str));
                } else if (i == -3) {
                    g.k(cVar, b);
                } else if (i == 2001) {
                    g.d().l(cVar, b, 2001);
                } else if (i == 11) {
                    g.d().l(cVar, b, 2000);
                }
            }
        }
    }
}
